package co.fitstart.market.web.controller.profile;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import co.fitstart.entity.market.MarketAchievement;
import co.fitstart.entity.market.MarketCourseRound;
import co.fitstart.entity.market.MarketWechatImage;
import co.fitstart.entity.user.User;
import co.fitstart.market.service.CoursewareService;
import co.fitstart.market.service.MarketAchievementService;
import co.fitstart.market.service.MarketCourseRoundService;
import co.fitstart.market.web.interceptor.MobileDetect;
import co.fitstart.market.web.interceptor.ParticipateDetect;
import co.fitstart.market.web.interceptor.Router;
import co.fitstart.market.web.interceptor.WechatAuthority;
import co.fitstart.market.web.resolver.Secure;

/**
 * 
 * @author houtao 、mohanlan
 *
 */
@Controller
@RequestMapping("/profile")
public class ProfileController {
    
    @Autowired
    private MarketCourseRoundService marketCourseRoundService;
    
    @Autowired
    private CoursewareService coursewareService;
    
    @Autowired
    private MarketAchievementService marketAchievementService;
    
    /**
     * Profile index page
     * 
     * @param user
     * @return
     */
    @RequestMapping(value="" , method = RequestMethod.GET )
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
    @ParticipateDetect
    public ModelAndView index(@Secure User user){
        
        ModelAndView modelAndView = new ModelAndView("profile/index");
        
        MarketCourseRound courseRound = marketCourseRoundService.load(user);
        modelAndView.addObject("courseRound", courseRound);
        
        modelAndView.addObject("user", user);
        
        return modelAndView;
    }
    
    /**
     * Profile community page
     * 
     * @return
     */
    @RequestMapping(value="/community" , method = RequestMethod.GET )
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
    @ParticipateDetect
    public ModelAndView community(@Secure User user){
        
        ModelAndView modelAndView = new ModelAndView("profile/communtity");
        
        MarketCourseRound courseRound = marketCourseRoundService.load(user);
        MarketWechatImage wechatImage = coursewareService.getMarketWechatImage(courseRound.getCourse());
        modelAndView.addObject("wechatImage", wechatImage);
        
        modelAndView.addObject("user", user);
        
        return modelAndView;
    }
    
    /**
     * Profile credit page
     * 
     * @param user
     * @return
     */
    @RequestMapping(value="/credit" , method = RequestMethod.GET )
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
    @ParticipateDetect
    public ModelAndView credit(@Secure User user){
        
        ModelAndView modelAndView = new ModelAndView("profile/credit");
        
        modelAndView.addObject("user", user);
        
        return modelAndView;
    }
    
    /**
     * Profile feedbacks page
     * 
     * @param user
     * @return
     */
    @RequestMapping(value="/feedbacks/create" , method = RequestMethod.GET )
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
    @ParticipateDetect
    public ModelAndView feedback(@Secure User user){
        
        ModelAndView modelAndView = new ModelAndView("profile/feedback-create");
        
        modelAndView.addObject("user", user);
        
        return modelAndView;
    }
    
    /**
     * @param user
     * @return
     */
    @RequestMapping(value="/achievements" , method = RequestMethod.GET )
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
    @ParticipateDetect
    public ModelAndView achievement(@Secure User user){
        
        ModelAndView modelAndView = new ModelAndView("profile/achievement");
        
        modelAndView.addObject("user", user);
        
        Map<String, MarketAchievement> achievements = marketAchievementService.list(user);
        modelAndView.addObject("achievements", achievements);
        
        return modelAndView;
    }
}
